package com.wcarmon.utils{

	import mx.core.mx_internal;
	import mx.logging.AbstractTarget;
	import mx.logging.ILogger;
	import mx.logging.LogEvent;
	
	use namespace mx_internal;

	public class GivonTraceTarget extends AbstractTarget{
		
		public var fieldSeparator:String = " ";
		public var includeTime : Boolean = true;
		public var includeLevel : Boolean = true;
		public var includeDate : Boolean = false;
		public var includeCategory : Boolean = true;
		
		public function GivonTraceTarget(){
			super();
		}

		override public function logEvent(event : LogEvent) : void {
			var date:String = "";
			
			if( includeDate || includeTime ){
    			var d:Date = new Date();
				if (includeDate){
    				date = Number(d.getUTCMonth() + 1).toString() + "/" +
					   d.getUTCDate().toString() + "/" + 
					   d.getUTCFullYear() + fieldSeparator;
    			}	
				if (includeTime){
    				date = pad(d.getUTCHours()) + ":" +
					   pad(d.getUTCMinutes()) + ":" +
					   pad(d.getUTCSeconds()) + "." +
					   pad(d.getUTCMilliseconds()) + fieldSeparator;
				}
			}
    	
			var level : String = "";
			if (includeLevel){
        		level = "[" + LogEvent.getLevelString(event.level) +
				    "]" + fieldSeparator;
        	}
			
			var category : String = includeCategory ?
							  ILogger(event.target).category + fieldSeparator :
							  "";

			internalLog(date + level + category + "- " + event.message);
		}


		mx_internal function internalLog(message:String):void {
			trace(message);
		}

		private function pad(num:Number):String{
        	return num > 9 ? num.toString() : "0" + num.toString();
		}		
		
	}
}